package com.wefans.wefansapp.common.tool.popup;

import java.util.Hashtable;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.wefans.wefansapp.R;
import com.wefans.wefansapp.common.tool.data.MFDataManager;
import com.wefans.wefansapp.common.tool.data.ReqCurrentPasswordData;
import com.wefans.wefansapp.common.tool.data.ReqJoinMemberData;
import com.wefans.wefansapp.common.tool.data.ResDefaultData;
import com.wefans.wefansapp.common.tool.debug.MFDebug;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager;
import com.wefans.wefansapp.common.tool.network.MFNetworkManager.IOnNetworkListener;
import com.wefans.wefansapp.common.tool.network.model.NetJsonData;
import com.wefans.wefansapp.common.tool.network.model.NetMessage;

public class ChangePasswordDialog extends BaseDialog {

	private String mStrOld = "";
	private String mStrPw1 = "";
	private String mStrPw2 = "";
	
	private Button mBtnClose = null;
	private EditText mEtOld = null;
	private EditText mEtNew = null;
	private EditText mEtConfirm = null;
	private Button mBtnChange = null;
	
	private OnChangePwListener mChangePwListener = null;
	
	public interface OnChangePwListener {
		void onChangePassword(String strOldPassword, String strNewPassword);
	}
	
	public ChangePasswordDialog(Context mContext, OnChangePwListener changePwListener) {
		super(mContext);
		
		setContentView(R.layout.dialog_change_password);
		
		mBtnClose = (Button)findViewById(R.id.BTN_DIALOG_CLOSE);
		mEtOld = (EditText)findViewById(R.id.ET_CURRENT_PW);
		mEtNew = (EditText)findViewById(R.id.ET_NEW_PW);
		mEtConfirm = (EditText)findViewById(R.id.ET_CONFIRM_PW);
		mBtnChange = (Button)findViewById(R.id.BTN_CHANGE_PASSWORD);
		
		mChangePwListener = changePwListener;
		mBtnClose.setOnClickListener(onClickListener);
		mBtnChange.setOnClickListener(onClickListener);
	}
	
	android.view.View.OnClickListener onClickListener = new android.view.View.OnClickListener() {
		
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.BTN_CHANGE_PASSWORD:
				mStrOld = mEtOld.getText().toString();
				mStrPw1 = mEtNew.getText().toString();
				mStrPw2 = mEtConfirm.getText().toString();
				
				if (mStrOld == null || mStrOld.length() <= 0 ||
					mStrPw1 == null || mStrPw1.length() <= 0 ||
					mStrPw2 == null || mStrPw2.length() <= 0) {
					return;
				}
			
				findViewById(R.id.TV_CHECK_PASSWORD_OK).setVisibility(mStrPw1.equals(mStrPw2) ? View.VISIBLE : View.GONE);
				findViewById(R.id.TV_CHECK_PASSWORD_CANCEL).setVisibility(mStrPw1.equals(mStrPw2) ? View.GONE : View.VISIBLE);
				
				if (!mStrPw1.equals(mStrPw2)) {
					return;
				}
				
				requestCurrentPassword(mStrOld);
				break;
			case R.id.BTN_DIALOG_CLOSE:
				dismiss();
				break;
			}
		}
	};
	
	private void requestCurrentPassword(String strCurrentPasswd) {
		String strUrl = MFNetworkManager.BASE_URL + "/my/get_pass.php";
		
		Hashtable<String, String> body = new Hashtable<String, String>();
		body.put("mem_idx", MFDataManager.getInstance(getContext()).getMebmerIdx());
		body.put("passwd_current", strCurrentPasswd);
		
		MFNetworkManager.getInstance(getContext()).requestPostData(MFNetworkManager.REQUEST_CURRENT_PASSWORD, strUrl, body, mNetworkListener);
	}
	
	IOnNetworkListener mNetworkListener = new IOnNetworkListener() {
		
		@Override
		public void onNetMessage(int resultCode, NetMessage message) {
			try {
	            //서버 여동 데이터
	            if( message instanceof NetJsonData ) {
	                long magicNum = ((NetJsonData) message).getMagicNum();
	                String json = ((NetJsonData) message).getResponseJson();
	                
	                if( json == null ) {
	                    throw new Exception("invalid json data");
	                }
	                
	                Gson gson = new Gson();
	                
	                ResDefaultData resultData = (ResDefaultData)gson.fromJson(json, ResDefaultData.class);
                    if( resultData == null ) {
                        throw new Exception("Gson parsing error");
                    }
	                
	                if (magicNum == MFNetworkManager.REQUEST_CURRENT_PASSWORD) {
	                		                		            		
	                	if (resultData.isSuccsess()) {
	                		if (mChangePwListener != null) {
	        					mChangePwListener.onChangePassword(mStrOld, mStrPw1);
	        					dismiss();
	        				}
	                	}
	                }
	            }	            
	        } catch(Exception e) {
	            MFDebug.e(e);
	        }		
		}
	};

}
